//
//  LoginService.swift
//

import MKit

class LoginService: NSObject {
    
    static let `default` = LoginService()
    private let LoginServiceKey = "LoginServiceKey"

    func save(_ model: LoginModel) {
        CacheManager<LoginModel>().setObject(model, forKey: LoginServiceKey)
    }
    
    var loginModel: LoginModel? {
        if let model = CacheManager<LoginModel>().object(forKey: LoginServiceKey) {
            return model
        }
        return nil
    }
    
    var ak: String {
        guard let model = loginModel else { return "" }
        return model.ak
    }
    
    var userInfo: UserInfoModel? {
        guard let model = loginModel else { return nil }
        return model.userInfo
    }
    
    var userProfile: UserProfileModel? {
        guard let model = loginModel else { return nil }
        return model.userProfile
    }
    
    var userId: String {
        guard let model = userInfo else { return "" }
        return model.id
    }
    
    var isLogin: Bool {
        if let _ = loginModel {
            return true
        }
        return false
    }
    
    func logout(_ isInvalid: Bool = true) {
        CacheManager<LoginModel>().removeAll()
    }
    
}
